package yxy.game.pm2.command.godBeast;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;


/**
 * @name CMD_PickedGodBeastInfo
 * @author wrs
 * @description 神兽槽信息
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastInfo,classtype = PickedGodBeastMessage.PickedGodBeastInfoRequest.class)
public class CMD_PickedGodBeastInfo extends Command<PickedGodBeastMessage.PickedGodBeastInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();

        PickedGodBeastMessage.PickedGodBeastInfoResponse.Builder response = PickedGodBeastMessage.PickedGodBeastInfoResponse.newBuilder();

        try {
            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在参数错误
                return;
            }
            //查看玩家背包是否有神兽令，有神兽令则修改神兽为可激活，并消耗一个神兽令，否则神兽不可激活
            Result result = new PickedGodBeastHandle().getGodBeastInfo(playerCode, response);
            if (!result.equals(Result.Success)){
                send(messageSource, response, Result.DataError);//不存在参数错误
                return;
            }

            send(messageSource,response, Result.Success);

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource, response, Result.DataError); //数据错误
        }

    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
